#!/bin/sh
# 编译安装管理DRBD
App=drbd
AppName=DRBD
AppBase=/App
AppDir=$AppBase/$App
AppProg=drbdadm
AppService=/etc/init.d/drbd
AppConf=$AppDir/etc/drbd.conf
Device=$(grep "^[[:space:]]*device" $AppConf 2> /dev/null | sed 's/;//g' | awk '{print $2}' | sed -n '1p')

AppSrcBase=/App/src
AppSrcFile=$App-*.tar.*
AppSrcDir=$(find $AppSrcBase -maxdepth 1 -name "$AppSrcFile" -type f 2> /dev/null | sed -e 's/.tar.*$//' -e 's/^.\///')

MountDir=/data
RemoveFlag=0
InstallFlag=0

# 获取PID
fpid()
{
    AppPids=$(ps ax | grep "\[drbd" | grep -v "grep" | awk '{print $1}' 2> /dev/null)
}

# 查询状态
fstatus()
{
    fpid

    if [ ! -d "$AppDir" ]; then
        echo "$AppName 未安装"
    else
        echo "$AppName 已安装"
        if [ -z "$AppPids" ]; then
            echo "$AppName 未启动"
        else
            echo "$AppName 正在运行"
        fi
    fi
}

# 删除
fremove()
{
    fpid
    RemoveFlag=1

    if [ -z "$AppPids" ]; then
        if [ -d "$AppDir" ]; then
            rm -rf $AppDir && echo "删除 $AppName"
            rm -f $AppService
        else
            echo "$AppName 未安装"
        fi
    else
        echo "$AppName 正在运行" && exit 1
    fi
}

# 备份
fbackup()
{
    Day=$(date +%Y-%m-%d)
    BackupFile=$App-$Day.tgz

    if [ -d "$AppDir" ]; then
        cd $AppBase
        tar zcvf $BackupFile --exclude=logs/* $App --backup=numbered
        [ $? -eq 0 ] && echo "$AppName 备份成功" || echo "$AppName 备份失败"
    else
        echo "$AppName 未安装"
    fi
}

# 安装
finstall()
{
    fpid
    InstallFlag=1

    if [ -z "$AppPids" ]; then
        test -d "$AppDir" && echo "$AppName 已安装"
        [ $? -ne 0 ] && fupdate && fcpconf
    else
        echo "$AppName 正在运行"
    fi
}

# 拷贝配置
fcpconf()
{
    cp -vf --backup=numbered $ScriptDir/$App.conf $AppConf
    cp -vf --backup=numbered $AppDir/etc/rc.d/init.d/drbd $AppService
    chmod u+x $AppService
    test -d $AppDir/var/run || mkdir -p $AppDir/var/run
}

# 更新
fupdate()
{
    Operate="更新"
    [ $InstallFlag -eq 1 ] && Operate="安装"
    [ $RemoveFlag -ne 1 ] && fbackup

    cd $AppSrcBase
    test -d "$AppSrcDir" && rm -rf $AppSrcDir

    tar zxf $AppSrcFile || tar jxf $AppSrcFile
    cd $AppSrcDir
    ./configure --prefix=$AppDir --with-km && make && make install

    if [ $? -eq 0 ]; then
        echo "$AppName $Operate成功"
    else
        echo "$AppName $Operate失败" 
        exit 1
    fi
}

# 初始化
finit()
{
    modprobe drbd
    $AppProg create-md all
}

# 设定为主服务器
fmaster()
{
    test -d $MountDir || mkdir -p $MountDir
    $AppProg primary all && mount $Device $MountDir
    [ $? -eq 0 ] && echo "设定为主 $AppName" || echo "设定为主 $AppName 失败"
}

# 设定为次服务器
fslave()
{
    test -d $MountDir && umount $MountDir
    $AppProg secondary all
    [ $? -eq 0 ] && echo "设定为次 $AppName" || echo "设定为次 $AppName 失败"
}

# 初始同步源
ffirst()
{
    $AppProg -- --overwrite-data-of-peer primary all
    [ $? -eq 0 ] && echo "初始同步源 $AppName" || echo "初始同步源 $AppName 失败"

}

# 格式化并使用设备
ffdisk()
{
    which mkfs.ext4 &> /dev/null && FsType=ext4 || FsType=ext3
    printf "格式化$Device(Yes)?" && read Format
    if [ "$Format" == "Yes" ]; then
        mkfs -t $FsType $Device && fmaster
    else
        echo "放弃格式化$Device"
    fi
}

ScriptDir=$(dirname $0)
ScriptFile=$(basename $0)
case "$1" in
    "install"   ) finstall;;
    "update"    ) fupdate;;
    "reinstall" ) fremove && finstall;;
    "remove"    ) fremove;;
    "backup"    ) fbackup;;
    "init"      ) finit;;
    "start"     ) $AppService start;;
    "stop"      ) $AppService stop;;
    "restart"   ) $AppService restart;;
    "status"    ) fstatus;;
    "cpconf"    ) fcpconf;;
    "reload"    ) $AppService reload;;
    "master"    ) fmaster;;
    "slave"     ) fslave;;
    "first"     ) ffirst;;
    "fdisk"     ) ffdisk;;
    *           )
    echo "$ScriptFile install              安装 $AppName"
    echo "$ScriptFile update               更新 $AppName"
    echo "$ScriptFile reinstall            重装 $AppName"
    echo "$ScriptFile remove               删除 $AppName"
    echo "$ScriptFile backup               备份 $AppName"
    echo "$ScriptFile init                 初始化 $AppName"
    echo "$ScriptFile start                启动 $AppName"
    echo "$ScriptFile stop                 停止 $AppName"
    echo "$ScriptFile restart              重启 $AppName"
    echo "$ScriptFile status               查询 $AppName 状态"
    echo "$ScriptFile cpconf               拷贝 $AppName 配置"
    echo "$ScriptFile reload               重载 $AppName 配置"
    echo "$ScriptFile master               设定为主 $AppName"
    echo "$ScriptFile slave                设定为次 $AppName"
    echo "$ScriptFile first                初始同步源 $AppName"
    echo "$ScriptFile fdisk                格式化并使用 $AppName"
    ;;
esac
